/*
 *  Java image creator class
 *  -------------------------
 *
 * Copyright (c) 2009, Martin Albrecht <martin.albrecht@javacoffee.de>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
 *     in the documentation and/or other materials provided with the distribution.
 *  - Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.imagecreator;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;


/*
 * Here, we have the ImageCreator class. This class implements no real constructor.
 * To initialize a picture, the Create method should be called. 
 */
public class ImageCreator {

    // Constants
    private final int LAYER_SIZE = 256;

    // Variables
    private int Height = 0;
    private int Width = 0;
    private Color BgColor = null;    

    private Layer ImageLayer[];
    private static int LayerIndex = 0;

    private RenderedImage rendImage;
    private Graphics2D g2d;
    private BufferedImage bufferedImage;

    private static String LastError = "No error so far...";

    public void Create(int w, int h, Color bgColor) {

        // Check Height and width parameters and set default values
        // if parameters < 0
        if(h>0) Height = h; else Height = 100;
        if(w>0) Width = w; else Width = 100;        

        BgColor = bgColor;        
        bufferedImage = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB);
        g2d = bufferedImage.createGraphics();

        // Create an array of image layers
        ImageLayer = new Layer[LAYER_SIZE];
        LayerIndex = 0;
    }           

    
    private void RenderImage() {
        
        g2d.setColor(BgColor);
        g2d.fillRect(0, 0, Width, Height);

        for(int i=0; i<LayerIndex; i++) {
            g2d.drawImage(ImageLayer[i].GetImage(), ImageLayer[i].GetX(), ImageLayer[i].GetY(), null);
        }                

        g2d.dispose();
        rendImage = bufferedImage;
    }

    
    public RenderedImage GetImage() {
        if(rendImage == null)
            RenderImage();
        
        return rendImage;
    }


    /*
     * Add a text layer
     */
    public void AddTextLayer(int w, int h, String Text, int tX, int tY, Color txColor, String font, int Size) {
    	ImageLayer[LayerIndex++] = new TextLayer(w, h, Text, tX, tY, txColor, font, Size);    	
    }
        
    /*
     * Add an image layer to the image
     */
    public void AddLayer(String filename) {
    	ImageLayer[LayerIndex++] = new ImageLayer(filename);
    }

    public void AddLayer(String filename, int x, int y) {
    	ImageLayer[LayerIndex++] = new ImageLayer(filename, x, y);
    }

    
    public int SaveImage(String filename, String type) {
        if(rendImage == null)
            RenderImage();
        
        try {
            // Save as PNG
            if(type.equals("png")) {
                File file = new File(filename+".png");
                ImageIO.write(rendImage, "png", file);
                return 0;
            }
            // Save as GIF
            else if(type.equals("gif")) {
                File file = new File(filename+".gif");
                ImageIO.write(rendImage, "gif", file);
                return 0;
            }
            // Save as JPEG
            else if(type.equals("jpg")) {
                File file = new File(filename+".jpg");
                ImageIO.write(rendImage, "jpg", file);
                return 0;
            }
            else {
                LastError = "Error: Filetype "+type+" unknown!";
                return 1;
            }
        } catch (IOException e) {
            LastError = e.getMessage();
            return 1;
        }
    }

    public String GetLastError(){
        return LastError;
    }
}
